package frobagroup.utils;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tinsley on 2016/7/13.
 */
@Data
@NoArgsConstructor
public class ResponseModel<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    private int code;
    private T data;
    private List<T> rows = new ArrayList<T>();

    public static <T> ResponseEntity<ResponseModel<T>> success(T result) {
        ResponseModel<T> responseModel = new ResponseModel<T>();
        responseModel.setData(result);
        ResponseEntity<ResponseModel<T>> responseEntity = new ResponseEntity<ResponseModel<T>>(responseModel,
                HttpStatus.OK);
        return responseEntity;
    }

    public static <T> ResponseEntity<ResponseModel<T>> success(T result,int code) {
        ResponseModel<T> responseModel = new ResponseModel<T>();
        responseModel.setData(result);
        responseModel.setCode(code);
        ResponseEntity<ResponseModel<T>> responseEntity = new ResponseEntity<ResponseModel<T>>(responseModel,
                HttpStatus.OK);
        return responseEntity;
    }

    public static <T> ResponseEntity<ResponseModel<T>> failed(HttpStatus status) {
        ResponseModel<T> responseModel = new ResponseModel<T>();
        ResponseEntity<ResponseModel<T>> responseEntity = new ResponseEntity<ResponseModel<T>>(responseModel, status);
        return responseEntity;
    }


}
